package com.eamois.dao;

import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import org.apache.commons.dbcp.BasicDataSource;

import com.mit.plm.session.ietm.Ietm;
import com.mit.plm.session.ietm.IetmHome;

public class DefaultDataSource extends BasicDataSource {
//	private SqlMix sqlMix;
	
	public DefaultDataSource() {
		ResourceBundle pro = ResourceBundle.getBundle("connjg");
		String cIP=pro.getString("CONNJGIP");
		String JBOSSPORT=pro.getString("JBOSSPORT");
		String DATABASENAME=pro.getString("DATABASENAME");
		String username=pro.getString("USERNAEM");
		String password=pro.getString("PASSWORD");
		java.util.Properties prop = System.getProperties();
		prop.setProperty("java.naming.factory.initial",
				"org.jnp.interfaces.NamingContextFactory");
		prop.setProperty("java.naming.provider.url", ""+cIP+":"+JBOSSPORT+""); 
		prop.setProperty("java.naming.factory.url.pkgs",
				"org.jboss.naming:org.jnp.interfaces");
		
		Ietm ietm = (Ietm) this.getEJBObject("Ietm", IetmHome.class);
		
//		try {
			String DataBaseUrl="jdbc:jtds:sqlserver://"+cIP+":1433;DatabaseName="+DATABASENAME+"";
			String DriverClassName="net.sourceforge.jtds.jdbc.Driver";

			this.setUrl(DataBaseUrl);
			this.setPassword(password);
			this.setUsername(username);
			this.setDriverClassName(DriverClassName);
			
//			this.setUrl(ietm.getDataBaseUrl());
//			this.setPassword(ietm.getDataBasePassword());
//			this.setUsername(ietm.getDataBaseUserName());
//			this.setDriverClassName(ietm.getDriverClassName());
//		} catch (RemoteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
	}
	
	protected Object getEJBObject(String ejbName, Class classEJBHome) {
		try {
			Context cxt = new InitialContext();
			Object obj = PortableRemoteObject.narrow(cxt.lookup(ejbName),
					classEJBHome);
			Method method = classEJBHome.getMethod("create", null);
			return method.invoke(obj, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
}
